package geektime.spring.springbucks.json;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import org.joda.money.Money;
import org.springframework.util.NumberUtils;
import org.springframework.util.StringUtils;

import java.io.IOException;

public class MoneyJsonDeserializer  extends JsonDeserializer<Money> {
    /**
     * 反序列化
     * 如果字符串中包含非数字，但又不能被解析则会抛出异常IllegalArgumentException
     */
    @Override
    public Money deserialize(JsonParser jp, DeserializationContext ctxt)
            throws IOException, JsonProcessingException {
        String text = jp.getText();
        Money money =  Money.parse(text);
        return money;
    }
}